package com.teamwork.module03.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.teamwork.common.entity.common.Message;
import com.teamwork.common.result.Result;
import com.teamwork.module03.mapper.MessageMapper;
import com.teamwork.module03.service.MessageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message> implements MessageService {
    /**
     * 保存审核结果
     *
     * @param status 审核转态（1，通过，2，未通过）
     * @param userId 给id的用户的消息
     * @param title
     */
    @Override
    public void addCheckResult(Integer status, Long userId, String title) {

        String message;
        if(status==1){
            message = "你发布的文章:"+title+"-已通过审核！";
        }else
             message = "你发布的文章:"+title+"-未通过审核！";


        Message msg =new Message();
        msg.setRemind("审核结果");
        msg.setMessage(message);
        msg.setUserid(Math.toIntExact(userId));
        msg.setStatus(0);

        boolean b = this.save(msg);

        if(!b){
            log.error("保存消息失败！！！");
        }


    }

    /**
     * 获取用户的所有消息
     * @param userid 用户id
     * @return 结果
     */
    @Override
    public Result getMsg(Integer userid) {

        LambdaQueryWrapper<Message> wrapper =new LambdaQueryWrapper<>();
        wrapper.eq(Message::getUserid,userid).orderByDesc(Message::getCreatetime);

        List<Message> list = this.list(wrapper);


        return Result.success(list);
    }

    /**
     * 用户确认消息
     * @param msgid 消息id
     * @return
     */
    @Override
    public Result readMsg(Integer msgid) {

        Message msg = this.getById(msgid);
        msg.setStatus(1);
        boolean b = this.updateById(msg);
        if(!b){
            log.error("消息确认出错！消息id:{}",msgid);
            return Result.failure("消息确认失败！后台错误！");
        }

        return Result.success("消息确认成功");
    }
}
